﻿using AEFunction;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using System;
using System.Windows.Forms;
namespace AETools
{
    public partial class ChooseMatchFields : Form
    {
        public string ShapeField;
        public string ExcelField;
        public ILayer[] Layers;
        public string[] ExcelFields;
        public string layerName;
        
        public ChooseMatchFields(ILayer[] layers, string[]excelFields)
        {
            InitializeComponent();
            Layers = layers;
            ExcelFields = excelFields;
            //comboBox1.Items.Clear();
            foreach (ILayer l in layers)
            {
                comboBox1.Items.Add((object)l.Name);
            }
            comboBox1.SelectedItem = comboBox1.Items[0];
            //listBox1.Items.Clear();
        
            //listBox2.Items.Clear();
            foreach (string s in excelFields)
            {
                listBox2.Items.Add((object)s);
            }
            ShapeField = ExcelField = "";
        }

        private void listBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            ShapeField = listBox1.SelectedItem.ToString();
        }

        private void listBox2_SelectedValueChanged(object sender, EventArgs e)
        {
            ExcelField = listBox2.SelectedItem.ToString();
        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            foreach (ILayer l in Layers)
            {
                if (l.Name == comboBox1.SelectedItem.ToString())
                {
                    IFeatureClass featureClass = ArcEngineFunction.GetLayerFeatureClass(l);
                    IFields layerFields = featureClass.Fields;
                    for (int i = 0; i < layerFields.FieldCount; i++)
                    {
                        IField field = layerFields.get_Field(i);
                        listBox1.Items.Add(field.Name);
                        listBox1.SelectedIndex = 0;
                    }
                }
            }
            layerName = comboBox1.SelectedItem.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
